<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<% 
session("redir")=""
session("ticketsaved")=0
fromdispatch=request.QueryString("FromDispatch")
'----------------- status -------------------------
set db=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
		  
filepath=server.MapPath("access/backend/admin.mdb")
if fromdispatch="yes" then
	criteria="'dispatch','both'"
else
	criteria="'operator','both'"
end if
		
sql="select status_name from dispatchcolors where type='status' order by status_name"
stats=""
statusoutput=""
stats=""
acceptablestatus=""
		  
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath
rs.cursorlocation=3
		  
rs.open sql,db,1,2
rs.activeconnection=nothing

do until rs.eof
	statusoutput=statusoutput & "<option value='" & rs("status_name") & "'>" & rs("status_name") & "</option>" & vbcrlf
	stats=stats & rs("status_name") & "|"
	rs.movenext
loop
stats=left(stats,len(stats)-1)
rs.close

sql="select status_name from dispatchcolors where [where] in (" & criteria & ") and type='status' order by status_name"

rs.open sql,db,1,2

rs.movefirst
do until rs.eof
	acceptablestatus=acceptablestatus & rs("status_name") & "|"
	rs.movenext
loop
acceptablestatus=left(acceptablestatus,len(acceptablestatus)-1)

rs.close
db.close
'------------------- end status --------------------

'------------------- begin tickets history-----------------
FilePath=server.MapPath("/afterhours/access/backend/tickets.mdb")
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath
ticketNum=request.QueryString("Ticket_ID")
if ticketNum<>"" then
	sql="select * from ticket_history where Ticket_ID=" & ticketnum
	rs.open sql,db,1,2
	if not rs.eof then
		x=0
		ra=rs.getrows() '0=Note_ID, 1=Ticket_ID, 2=User, 3=When, 4=Note
		rs.close
		rowcount=ubound(ra,2)
		do until x>rowcount
			notesoutput=notesoutput & "<tr><td bgcolor='#D2FFD2' class='TableTitles'>" & x+1 & ") " & ra(2,x) & " &nbsp; " & ra(3,x) & "</td><td bgcolor='#D2FFD2' class='TableTitles'>" & ra(4,x) & "</td></tr>" & vbcrlf
		x=x+1
		loop
		
	else
		rowcount=0
		notesoutput="<tr><td>(This Ticket Has No History)</td></tr>"
	end if
	'---------------- end ticket history -----------------------
	
	'---------------- begin ticket --------------------------
	sql="select * from tickets where ticket_id=" & ticketNum
	set trs=server.CreateObject("adodb.recordset")
	'if rs.state=1 then rs.close
	trs.open sql,db,1,2
	ra=trs.getrows() '0=Ticket_ID, 1=Contract_ID, 2=Status, 3=Given_To, 4=Created_Time, 5=Completed_Time, 6=Appliance, 7=Emergency, 8=follow up
end if
	'------------------ end ticket -----------------------
	'------------------ end ASP --------------------------	
%>	
	
<HTML XMLNS="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" />
<TITLE>Ticket Window</TITLE>
<LINK HREF="UniversalStyleSheet.css" REL="stylesheet" TYPE="text/css" />
<STYLE TYPE="text/css">
<!--
body {
	margin-left: 1px;
	margin-top: 1px;
	margin-right: 1px;
	margin-bottom: 1px;
}

-->
</STYLE>

<SCRIPT TYPE="text/javascript">
function FrmSubmit()
    {
	var CID=document.getElementById("Contract_ID").value;
	var Appl=document.getElementById("Appliance").selectedIndex;
	var Emerg=document.getElementById("Emergency").value;
	var Note=document.getElementById("Notes").value;
	var stat=document.getElementById("Status").selectedIndex;
	var msg="";
	<%
	if fromdispatch="yes" then
		%>
		var distime=document.getElementById("GivenToTime").value;
		<%
	end if
	%>

	var emer=document.getElementById("Emergency");
	var tf=document.getElementById("TicketForm");

	if (CID=="") {
		alert ("You Have Not Selected An Account To Save The Ticket To. \n If You've Already Pulled Up An Account Then Click 'New Ticket' To Associate the Contract ID With This Ticket.");
 		return;
	}//end if

	if (stat==-1 || stat==0) {msg += "- Status \n";}

	if (Appl==-1 || Appl==0){ msg += "- Appliance \n";}

	if (Emerg != "Yes" && Emerg != "No") {msg += "- Emergency \n";}

	if (Note=="") {msg += "- Notes \n";}
	

	if (msg != ""){
		alert ("Cant Save Changes. The Following Required Fields Were Blank:");
		return;
	}//end if

	tf.onsubmit ="";
	<% 
	if fromdispatch="yes" then 
		%>
		document.TicketForm.action="SaveTicket.asp?FromDispatch=yes&user=<%= request.Cookies("Hsuser") %>";
		<% 
	else 
		randomize() 'this randomized number is to make the saveticket url unique so the IE bug doesnt run the code twice
		uniqueid=hex(100000000 * rnd)
		%>
		tf.action="SaveTicket.asp?uid=<%= uniqueid %>&user=<%= request.Cookies("Hsuser") %>"
		<% 
	end if 
	%>
	tf.submit();
    }

function status_change(){
	var stat=document.getElementById("status");
	var oldstat=document.getElementById("statusPH");
	
	var acceptable=0;
	
	var accpt="<%= acceptablestatus %>";
	var accptarray=accpt.split("|");
	
	for (x=0;x< accptarray.length;x++)
	    {
		if (stat.value == accptarray[x]){acceptable=1; break;}
	    }
	
	if (acceptable == 0){
		alert("Setting this ticket to a status of '" + stat.value + "' is not allowed in this screen.");
		stat.selectedindex=oldstat.value
		return;
	}//end if
	
	oldstat.value=stat.selectedindex
	
    }//end function
</SCRIPT>

</HEAD>

<BODY>
<FORM ID="TicketForm" NAME="TicketForm" METHOD="post" ACTION="SaveTicket.asp" onSubmit="javascript: return false;">
<TABLE WIDTH="600" BORDER="0" CELLSPACING="0" CELLPADDING="0">
 <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperLeft.png"></TD>
    <TD WIDTH="560" HEIGHT="20" BACKGROUND="Images/TableUpperMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleLeft.png"></TD>
    <TD WIDTH="460" BGCOLOR="#FFFFFF">
      <TABLE WIDTH="560" BORDER="0" CELLPADDING="0" CELLSPACING="3" BGCOLOR="#FFFFFF">
        <TR>
          <TD WIDTH="235" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Contract ID
             
            <INPUT NAME="Contract_ID_Display" TYPE="text" ID="Contract_ID_Display" SIZE="15" disabled="disabled"/>
             
            <INPUT NAME="Contract_ID" TYPE="hidden" ID="Contract_ID" />
         <BR /></TD>
          <TD WIDTH="316" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Ticket Number 
             
            <INPUT NAME="Ticket_ID_Display" TYPE="text" ID="Ticket_ID_Display" SIZE="15" disabled="disabled"/>
            <INPUT NAME="Ticket_ID" TYPE="hidden" ID="Ticket_ID" />
         </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText">Appliance 
            
            <SELECT NAME="Appliance" ID="Appliance" TABINDEX="1">
              <%
			  dim fs,tf
			  set fs=server.CreateObject("scripting.filesystemobject")
			  set tf=fs.opentextfile(server.MapPath("access/backend/appliances.txt"))
			  do until tf.atendofstream
			  	appl=tf.readline
			  	response.Write("<option value='" & appl & "'>" & appl & "</option>")
				appliances=appliances & appl & "|"
			  loop
			  appliances=left(appliances,len(appliances)-1)
			  tf.close
			  set tf=nothing
			  set fs=nothing
			  %>	
			  
            </SELECT>
            </TD>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText"><SPAN CLASS="style10"># of Calls</SPAN>
            <INPUT NAME="CallNumber" TYPE="text" ID="CallNumber" SIZE="1" disabled="disabled"/>
            &nbsp; &nbsp;<SPAN CLASS="style10">Status</SPAN>
		  
		  
            <SELECT NAME="Status" ID="Status" TABINDEX="2" onChange="status_change()">
              <OPTION selected="selected">Choose...</OPTION>
			<%= statusoutput %>
            </SELECT>
		  
		  
		  <INPUT NAME="statusPH" TYPE="hidden" ID="statusPH">
            </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText">Emergency: 
             
			  &nbsp;
              <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="Yes" />
              Yes  
              &nbsp; &nbsp; &nbsp;
			  <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="No" />
               
              No 
            <BR />            
	    </TD>
          <TD  bgcolor="#D2FFD2" CLASS="SearchFormText">
			<P>Given To
			  <% if fromdispatch<>"yes" then %>
			  	<INPUT NAME="GivenTo" TYPE="text" disabled="disabled" ID="GivenTo" TABINDEX="4"/>
			  <% else %>
			  	
			  	<INPUT NAME="GivenTo" TYPE="text" SIZE="20" ID="GivenTo" TABINDEX="4" />									
			  	<BR/>
			  	Dispatched Time 
			  	<INPUT NAME="GivenToTime" TYPE="text" SIZE="20" ID="GivenToTime" />
				<BR/>
				Follow Up Time
				<INPUT NAME="FollowUp" TYPE="text" SIZE="20" ID="FollowUp" />
		      <% end if %>
		    </P>
         </TD>
        </TR> 
        <TR>
          <TD COLSPAN="2" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Notes 
             
            <TEXTAREA NAME="Notes" COLS="50" ROWS="5" ID="Notes"></TEXTAREA>
             </TD>
          </TR>
        <TR>
          <TD HEIGHT="10">&nbsp;</TD>
          <TD HEIGHT="10" ALIGN="right">
          <INPUT TYPE="image" NAME="Save" SRC="Images/SaveButton.png" ALT="save" WIDTH="60" HEIGHT="30" BORDER="0"  onclick="FrmSubmit()"/> 
		</TD>
        </TR>
		<%= notesoutput %>
		
<% if ticketnum<>"" then %>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ra(1,0) %>"
		document.getElementById("Contract_ID").value="<%= ra(1,0) %>"
		document.getElementById("Ticket_ID_Display").value="<%= ra(0,0) %>"
		document.getElementById("Ticket_ID").value="<%= ra(0,0) %>"
		document.getElementById("CallNumber").value="<%= rowcount+1 %>"
		<% if request.QueryString("FromDispatch")<>"yes" then %> 
			document.getElementById("GivenTo").value="<%= ra(3,0) & " : " & ra(5,0)%>"
		<% else %>
			document.getElementById("GivenTo").value="<%= ra(3,0) %>"
			document.getElementById("GivenToTime").value="<%= ra(5,0) %>"
			document.getElementById("FollowUp").value="<%= ra(8,0) %>"
		<% end if %>
		 <% 'appliance list with selection based on chosen appliance
			x=0
			appliance=split(appliances,"|")
			do while x<=ubound(appliance)
				if ra(6,0)=appliance(x) then
					%>
					document.getElementById("Appliance").selectedIndex=<%= x %>
					<%
					exit do
				end if
				x=x+1
			loop
							
				
			'------------- set status to ticket status
			x=0
			statuses=stats
			statarray=split(statuses,"|")
			do while x<=ubound(statarray)
				if ra(2,0)=statarray(x) then
					%>
					document.getElementById("Status").selectedIndex=<%= x+1 %>
					document.getElementById("statusPH").value= "<%= x+1 %>"
					<%
					
					exit do
				end if
				x=x+1
			loop
			
			
			'------------- emergency radio buttons
			%>
			
			<%
			if ra(7,0)="Yes" then
				%>
				document.TicketForm.Emergency[0].checked=true
				<%
			end if
			
			if ra(7,0)="No" then
				%>
				document.TicketForm.Emergency[1].checked=true
				<%
			end if
					  
		%>
	</SCRIPT>
<%
end if 'the if that checks if ticketnum<>""

ContractId=request.QueryString("Contract_ID")
if ContractID<>"" then
	%>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ContractId %>"
		document.getElementById("Contract_ID").value="<%= ContractId %>"
		document.getElementById("CallNumber").value=1
	</SCRIPT>

	<%
end if 'for if contractid<>""

if isobject("trs") then 
	if trs.state=1 then trs.close
end if

set trs=nothing

if rs.state=1 then rs.close
set rs=nothing

db.close
set db=nothing
%>
	</TABLE>
            
    </TD>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerLeft.png"></TD>
    <TD WIDTH="560" HEIGHT="20" BACKGROUND="Images/TableLowerMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerRight.png"></TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
